package com.szr.leex.dao.converter.type.java;

import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.AbstractTypeDescriptor;
import org.hibernate.type.descriptor.java.StringTypeDescriptor;

import com.szr.leex.po.enums.WeekEnum;

/**
 * week enum java type descriptor
 * Created by WangChen on 2018/9/9 17:02.
 */
public class WeekTypeDescriptor extends AbstractTypeDescriptor<WeekEnum> {

    public static final WeekTypeDescriptor INSTANCE = new WeekTypeDescriptor();

    WeekTypeDescriptor(){
        super(WeekEnum.class);
    }
    protected WeekTypeDescriptor(Class<WeekEnum> type) {
        super(type);
    }

    @Override
    public String toString(WeekEnum value) {
        return value == null ? null : value.getCode();
    }

    @Override
    public WeekEnum fromString(String string) {
        return string == null ? null : WeekEnum.valueOf(string);
    }

    @Override
    public <X> X unwrap(WeekEnum value, Class<X> type, WrapperOptions options) {
        return StringTypeDescriptor.INSTANCE.unwrap(
                value == null ? null : value.getCode(),
                type,
                options
        );
    }

    @Override
    public <X> WeekEnum wrap(X value, WrapperOptions options) {
        return WeekEnum.getEnumInfo(StringTypeDescriptor.INSTANCE.wrap(value, options));
    }
}
